package com.wechat.android.app.db;
import com.wechat.android.app.global.Constants;

import org.xutils.DbManager;
import org.xutils.x;

/**
 * Created by wangyang on 2017/12/12 0030.
 */
public class DbHelper {
    private final String TAG = getClass().getSimpleName();
    private static DbHelper mInstance = null;
    private static DbManager.DaoConfig mDaoConfig =null;
    public static synchronized DbHelper getInstance (){
        if(mInstance==null){
            mInstance = new DbHelper();
        }
        return mInstance;
    }
    private DbManager.DaoConfig initConfig(){
        return new DbManager.DaoConfig()
                .setDbName(Constants.DB_NAME)
                .setDbVersion(Constants.APP_DB_VERSION)
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

                    }
                });
    }
    public DbManager getDb(){
        if(mDaoConfig==null){
            mDaoConfig = initConfig();
        }
        return x.getDb(mDaoConfig);
    }

}
